package com.bauer.base.io;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

/**
 * @Author ：rui.wei
 * @Date ：Created in 14:12 2019/6/19
 * @Description：
 */
public class PipeDemo {

    private static final PipedInputStream pipedInputStream  = new PipedInputStream();

    private static final PipedOutputStream pipedOutputStream =  new PipedOutputStream();

    public static void main(String[] args) throws Exception{
        pipedInputStream.connect(pipedOutputStream);

        Thread thread1 =  new Thread(()->{
            try {
                pipedOutputStream.write("asdfsda".getBytes());
                pipedOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        Thread thread2 =  new Thread(()->{
            byte[] bytes =  new byte[1024];
            int len =0;
            try {
                while (( len = pipedInputStream.read(bytes) )!= -1){
                    System.out.println(new String(bytes,0,len));
                }
                pipedInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        thread1.start();
        thread2.start();
    }
}
